package cn.com.bluemoon.daps.model.core.file.validator;

import cn.com.bluemoon.daps.model.service.impl.DapDataModelGroupServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * 表分组校验（当前校验只允许默认组）
 *
 * @author Jarod.Kong
 * @date 2021/3/15 16:07
 */
@Slf4j
@Deprecated
public class MyTableGroupValidator implements ConstraintValidator<MyTableGroup, String> {
    private DapDataModelGroupServiceImpl.NotDwSysGroup[] notDwSysGroups;
    private DapDataModelGroupServiceImpl.DwGroup[] dwGroups;

    @Override
    public void initialize(MyTableGroup tableGroup) {
        dwGroups = DapDataModelGroupServiceImpl.DwGroup.values();
        notDwSysGroups = DapDataModelGroupServiceImpl.NotDwSysGroup.values();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }

        if (Arrays.stream(dwGroups).anyMatch(g -> g.getName().equalsIgnoreCase(value.trim()))) {
            log.debug("命中数仓的默认组");
            return true;
        }
        if (Arrays.stream(notDwSysGroups).anyMatch(g -> g.getName().contentEquals(value.trim()))) {
            log.debug("命中非数仓的默认组");
            return true;
        }
        log.warn("{}无法匹配到对应组名称", value);
        return false;
    }
}
